﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;
using NetJsWire.JsonRpcHandler;
using Newtonsoft.Json;

namespace NetJsWire.Widgets.Config
{
    public class JsonStoreConfigOptions : StoreConfigOptions
    {
        private string _idProperty = "id";
        private string _root = "undefined";
        private string _successProperty = "success";
        private string _totalProperty = "total";

        public JsonStoreConfigOptions( IWidget ownerWidget )
            : base( ownerWidget )
        {
            _xtype = "jsonstore";
        }

        [DefaultValue( "jsonstore" )]
        [JsonProperty( "xtype", DefaultValueHandling = DefaultValueHandling.Include, NullValueHandling = NullValueHandling.Ignore )]
        public override string Xtype
        {
            get { return base.Xtype; }
            set { }
        }

        [DefaultValue( "id" )]
        [JsonProperty( "idProperty", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "[id] Name of the property within a row object that contains a record identifier value. Defaults to id" )]
        public override string IdProperty
        {
            get { return _idProperty; }
            set
            {
                SetValue( "idProperty", value, "id", _idProperty );
                _idProperty = value;
            }
        }

        [DefaultValue( "undefined" )]
        [JsonProperty( "root", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "[undefined] Required. The name of the property which contains the Array of row objects. Defaults to undefined. An exception will be thrown if the root property is undefined." )]
        public override string Root
        {
            get { return _root; }
            set
            {
                SetValue( "root", value, "undefined", _root );
                _root = value;
            }
        }

        [DefaultValue( "success" )]
        [JsonProperty( "successProperty", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "[undefined] Required. The name of the property which contains the Array of row objects. Defaults to undefined. An exception will be thrown if the root property is undefined." )]
        public override string SuccessProperty
        {
            get { return _successProperty; }
            set
            {
                SetValue( "successProperty", value, "success", _successProperty );
                _successProperty = value;
            }
        }

        [DefaultValue( "total" )]
        [JsonProperty( "totalProperty", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [Description( "[total] Name of the property from which to retrieve the total number of records in the dataset. This is only needed if the whole dataset is not passed in one go, but is being paged from the remote server. Defaults to total." )]
        public override string TotalProperty
        {
            get { return _totalProperty; }
            set
            {
                SetValue( "totalProperty", value, "total", _totalProperty );
                _totalProperty = value;
            }
        }
    }
}
